package com.zhousb.deviceManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zhousb.deviceManager.mapper.WxUserConfigMapper;
import com.zhousb.deviceManager.model.entity.KtWxUserConfig;
import com.zhousb.deviceManager.service.WxUserConfigService;
import com.zhousb.deviceManager.utils.IdGenerator;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class WxUserConfigServiceImpl implements WxUserConfigService {

    @Resource
    private WxUserConfigMapper wxUserConfigMapper;

    @Override
    public KtWxUserConfig getUserConfig(String phone) {
        LambdaQueryWrapper<KtWxUserConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KtWxUserConfig::getPhone, phone);
        return wxUserConfigMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean createUserConfig(String phone) {
        LambdaQueryWrapper<KtWxUserConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KtWxUserConfig::getPhone, phone);
        boolean isExists = wxUserConfigMapper.exists(queryWrapper);
        if (!isExists) {
            KtWxUserConfig wxUserConfig = new KtWxUserConfig();
            wxUserConfig.setId(IdGenerator.nextId());
            wxUserConfig.setPhone(phone);
            wxUserConfigMapper.insert(wxUserConfig);
        }
        return true;
    }

    @Override
    public boolean setUserConfig(KtWxUserConfig bo) {
        LambdaUpdateWrapper<KtWxUserConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(KtWxUserConfig::getPhone, bo.getPhone())
                .set(KtWxUserConfig::getBarCode, bo.getBarCode())
                .set(KtWxUserConfig::getQrCode, bo.getQrCode())
                .set(KtWxUserConfig::getDataMatrixCode, bo.getDataMatrixCode())
                .set(KtWxUserConfig::getPdf417Code, bo.getPdf417Code())
                .set(KtWxUserConfig::getAllowPhoneImage, bo.getAllowPhoneImage())
                .set(KtWxUserConfig::getAllowUnknownCode, bo.getAllowUnknownCode())
                .set(KtWxUserConfig::getEmail, bo.getEmail());
        wxUserConfigMapper.update(updateWrapper);
        return true;
    }
}
